<?php

namespace Crc\Entity;

use Doctrine\ORM\Mapping AS ORM,
    Doctrine\Common\Annotations\AnnotationRegistry,
    Doctrine\Common\Collections,
    Crc\Annotations\Filters as Filter,
    Crc\Annotations\Validators as Assert,
    Crc\Annotations\Form\Elements as Element,
    Crc\Annotations\Grid as Grid;

/**
 *
 *
 * @ORM\Entity
 * @ORM\Table(name="acl_role")
 * @Grid\QuickActions(actions={"active", "inactive"})
 */

class AclRole extends AbstractEntity
{
    /**
     * Default statuses
     */
    const STATUS_DELETED  = -1;
    const STATUS_INACTIVE =  0;
    const STATUS_ACTIVE   =  1;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @Grid\Column(label="ID", show=true, default=false, sortable=true, order="ASC")
     * @Grid\Filter(name="number", label="ID")
     * @var int
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="AclRole", inversedBy="children")
     * @ORM\JoinColumn(name="id_parent", referencedColumnName="id")
     * @Grid\Column(label="Parent", show=true, default=true, sortable=true)
     * @Grid\Filter(name="entity", label="Parent", entity="AclRole")
     */
    protected $parent;

    /**
     * @ORM\OneToMany(targetEntity="AclRole", mappedBy="parent")
     * @Grid\Column(show=false)
     */
    protected $children;

    /**
     * @ORM\OneToMany(targetEntity="AclRight", mappedBy="role")
     * @Grid\Column(show=false)
     */
    protected $rights;

    /**
     * @ORM\OneToMany(targetEntity="User", mappedBy="role")
     * @Grid\Column(show=false)
     */
    protected $users;

    /**
     * @ORM\Column(type="string")
     * @Grid\Column(label="Name", show=true, default=true, sortable=true)
     * @Grid\Filter(name="text", label="Name")
     */
    protected $name;

    public function __construct()
    {
        $this->children = new ArrayCollection();
        $this->rights = new ArrayCollection();
        $this->users = new ArrayCollection();
    }

    public function addChild(AclRole $_child) {
         $this->children->add($_child);
         $_child->setParent($this);
         return $this;
    }

    public function setParent(AclRole $_parent = null)
    {
         $this->parent = $_parent;
         return $this;
    }

    public function addRights(AclRight $_right)
    {
        $this->rights->add($_right);
        $_right->setRole($this);
        return $this;
    }

    public function addUser(User $_user)
    {
        $this->users->add($_user);
        $_user->setRole($this);
        return $this;
    }
}